<?php
/**
 * ClassName:渠道接口
 * @auditor:leijunxiang
 * @time 2025/4/2 10:42
 */

namespace app\controller\freightForward;

use app\BaseController;
use app\factory\model\RequestAgreement;
use app\factory\model\ResponseAgreement;
use app\tool\freightForward\ChannelHandle;
use think\facade\Log;

class ChannelController extends BaseController
{
    /**
     * 查询
     * @return void
     */
    public function select($forwardId,$page,$limit)
    {

        $response = app(ResponseAgreement::class);
        $forwardId = isset($forwardId) ? $forwardId : '';
        if (empty($forwardId)) {
            $response->setErrCode(FAIL);
            $response->setErrMsg("请输入货代id！");
            return;
        }
        $handle = app(ChannelHandle::class);
        //查询渠道(货代id)
        $data = $handle->selectChannel($forwardId,$page,$limit);
        if (empty($data)) {
            $response->setErrCode($handle->getErrCode());
            $response->setErrMsg($handle->getErrMsg());
        } else {
            $response->setErrCode(SUCCESS);
            $response->setErrMsg("");
            $response->setContent($data);
        }

    }


    /**
     * 新增
     * @return void
     */
    public function add()
    {
        $responseObj = app(ResponseAgreement::class);
        $requestAgreement = app(RequestAgreement::class);
        $content = $requestAgreement->getContent();
        $channelName = isset($content['channelName']) ? $content['channelName'] : '';
        if (empty($channelName)) {
            $responseObj->setErrCode(FAIL);
            $responseObj->setErrMsg("请输入渠道名称！");
            return;
        }
        $forwardId = isset($content['forwardId']) ? $content['forwardId'] : '';
        if (empty($forwardId)) {
            $responseObj->setErrCode(FAIL);
            $responseObj->setErrMsg("请输入货代id！");
            return;
        }
        $channelHandle = app(ChannelHandle::class);
        $flag = $channelHandle->addChannel($forwardId, $channelName);
        if (!$flag) {
            $responseObj->setErrCode($channelHandle->getErrCode());
            $responseObj->setErrMsg($channelHandle->getErrMsg());
        } else {
            $responseObj->setErrCode(SUCCESS);
            $responseObj->setErrMsg("");
            $responseObj->setContent($flag);
        }
    }

    public function update()
    {
        $responseObj = app(ResponseAgreement::class);
        $requestAgreement = app(RequestAgreement::class);
        $content = $requestAgreement->getContent();
        $channelId = isset($content['channelId']) ? $content['channelId'] : '';
        if (empty($channelId)) {
            $responseObj->setErrCode(FAIL);
            $responseObj->setErrMsg("请输入渠道id！");
            return;
        }
        $channelName = isset($content['channelName']) ? $content['channelName'] : '';
        if (empty($channelName)) {
            $responseObj->setErrCode(FAIL);
            $responseObj->setErrMsg("请输入渠道名称！");
            return;
        }
        $channelHandle = app(ChannelHandle::class);
        $flag = $channelHandle->updateChannel($channelId,$channelName);
        if (!$flag) {
            $responseObj->setErrCode($channelHandle->getErrCode());
            $responseObj->setErrMsg($channelHandle->getErrMsg());
        } else {
            $responseObj->setErrCode(SUCCESS);
            $responseObj->setErrMsg("更新成功");
        }
    }

    public function del()
    {
        $responseObj = app(ResponseAgreement::class);
        $requestObj = app(RequestAgreement::class);
        $attr = $requestObj->getContent()['channelIdAttr'];
        if (empty($attr)) {
            $responseObj->setErrCode(FAIL);
            $responseObj->setErrMsg("请输入渠道id！");
            return;
        }
        $channelHandle =app(ChannelHandle::class);
        $flag = $channelHandle->delChannel($attr);
        if (!$flag) {
            $responseObj->setErrCode($channelHandle->getErrCode());
            $responseObj->setErrMsg($channelHandle->getErrMsg());
        } else {
            $responseObj->setErrCode(SUCCESS);
            $responseObj->setErrMsg("删除成功");
        }
    }

}